package com.LC.Service.Impl;

import com.LC.DAO.PermissionDAO;
import com.LC.Entity.PageResult;
import com.LC.Entity.Permission;
import com.LC.Entity.QueryPageBean;
import com.LC.Service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
  
    @Autowired
    PermissionDAO permissionDAO;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDAO.findPage(queryString);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        System.out.println(pageResult);
        return pageResult;
    }

  /**
   * 新增权限
   * @param permission
   */
    @Override
    public void addPermission(Permission permission) {
      try {
        permissionDAO.addPermission(permission);
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
      }
    }
  
    /**
     * 删除权限
     * @param id
     */
    @Override
    public void deletePermission(int id) {
      try {
        permissionDAO.deletePermission(id);
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
      }
    }

  /**
   * 根据Id查找，用于回显数据
   * @param id
   * @return
   */
    @Override
    public Permission dataById(int id) {
        Permission permission = null;
        try {
            permission = permissionDAO.dataById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return permission;
    }

    /**
     * 更新权限信息
     * @param permission
     */
    @Override
    public void updatePermission(Permission permission) {
        try {
            permissionDAO.updatePermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有权限，集合封装返回
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDAO.permissionList();
    }

}
